package com.ytzl.itrip;

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sam on 2018/2/9.
 */
public class MvcHandler {
    public List<Table> tables;

    public List<Table> getTables() {
        return tables;
    }

    private TableHandler tableHandler;

    //程序包地址
    private final String packagePath = "com.ytzl.itrip";

    //模板的地址
    private final String templatePath = "G:\\课件\\itrip\\project\\itripbackend\\codeparent\\src\\main\\java\\com\\ytzl\\itrip\\templates";

    //代码生成地址
    private final String modelPath = "G:\\itrip\\model";
    private final String mapperXmlPath = "G:\\itrip\\mapper-xml";
    private final String mapperInterfacePath = "G:\\itrip\\mapper-interface";
    private final String servicePath = "G:\\itrip\\service";
    private final String serviceImplPath = "G:\\itrip\\service-impl";

    public MvcHandler() {
        tableHandler = new TableHandler();
        //获取所有表数据
        tables = tableHandler.getTables();
    }

    /**
     * 执行生成实体类
     *
     * @param table
     */
    public void executeModule(Table table) {
        // step1 创建freeMarker配置实例
        Configuration configuration = new Configuration();
        Writer out = null;
        try {
            // step2 获取模版路径
            configuration.setDirectoryForTemplateLoading(new File(templatePath));
            // step3 创建数据模型
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("table", table);
            dataMap.put("package", packagePath);
            // step4 加载模版文件
            Template template = configuration.getTemplate("model.ftl");
            // step5 生成数据
            File docFile = new File(modelPath + "\\" + table.getClassName() + ".java");
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
            // step6 输出文件
            template.process(dataMap, out);
            System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^ " + table.getClassName() + "文件创建成功 !");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != out) {
                    out.flush();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

    /**
     * 执行生成mapper接口
     *
     * @param table
     */
    public void executeMapper(Table table) {
        // step1 创建freeMarker配置实例
        Configuration configuration = new Configuration();
        Writer out = null;
        try {
            // step2 获取模版路径
            configuration.setDirectoryForTemplateLoading(new File(templatePath));
            // step3 创建数据模型
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("table", table);
            dataMap.put("package", packagePath);
            // step4 加载模版文件
            Template template = configuration.getTemplate("mapper-interface.ftl");
            // step5 生成数据
            File docFile = new File(mapperInterfacePath + "\\" + table.getClassName() + "Mapper.java");
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
            // step6 输出文件
            template.process(dataMap, out);
            System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^ " + table.getClassName() + "Mapper 文件创建成功 !");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != out) {
                    out.flush();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

    /**
     * 执行生成mapper接口对应的xml
     *
     * @param table
     */
    public void executeMapperXml(Table table) {
        // step1 创建freeMarker配置实例
        Configuration configuration = new Configuration();
        Writer out = null;
        try {
            // step2 获取模版路径
            configuration.setDirectoryForTemplateLoading(new File(templatePath));
            // step3 创建数据模型
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("table", table);
            dataMap.put("package", packagePath);
            // step4 加载模版文件
            Template template = configuration.getTemplate("mapper-xml.ftl");
            // step5 生成数据
            File docFile = new File(mapperXmlPath + "\\" + table.getClassName() + "Mapper.xml");
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
            // step6 输出文件
            template.process(dataMap, out);
            System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^ " + table.getClassName() + "Mapper.xml 文件创建成功 !");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != out) {
                    out.flush();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

    /**
     * 执行生成service接口
     *
     * @param table
     */
    public void executeService(Table table) {
        // step1 创建freeMarker配置实例
        Configuration configuration = new Configuration();
        Writer out = null;
        try {
            // step2 获取模版路径
            configuration.setDirectoryForTemplateLoading(new File(templatePath));
            // step3 创建数据模型
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("table", table);
            dataMap.put("package", packagePath);
            // step4 加载模版文件
            Template template = configuration.getTemplate("service-interface.ftl");
            // step5 生成数据
            File docFile = new File(servicePath + "\\" + table.getClassName() + "Service.java");
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
            // step6 输出文件
            template.process(dataMap, out);
            System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^ " + table.getClassName() + "Service.java 文件创建成功 !");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != out) {
                    out.flush();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

    /**
     * 执行生成service接口Impl实现类
     *
     * @param table
     */
    public void executeServiceImpl(Table table) {
        // step1 创建freeMarker配置实例
        Configuration configuration = new Configuration();
        Writer out = null;
        try {
            // step2 获取模版路径
            configuration.setDirectoryForTemplateLoading(new File(templatePath));
            // step3 创建数据模型
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("table", table);
            dataMap.put("package", packagePath);
            // step4 加载模版文件
            Template template = configuration.getTemplate("service-impl.ftl");
            // step5 生成数据
            File docFile = new File(serviceImplPath + "\\" + table.getClassName() + "ServiceImpl.java");
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
            // step6 输出文件
            template.process(dataMap, out);
            System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^ " + table.getClassName() + "ServiceImpl.java 文件创建成功 !");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != out) {
                    out.flush();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
}
